/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.v1.jitclouds.mda.providers;

import br.ufba.lasid.v1.jitclouds.mda.MDA;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.BasicHypervisor;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.IHypervisor;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.IVirtualMachine;
import java.util.List;
import java.util.Properties;

/**
 *
 * @author aliriosa
 */
public class HypervisorProvider implements IProvider<IHypervisor>{
    
    @Override
    public IHypervisor create(Properties options) throws Exception {
        
        return prepare(new BasicHypervisor(), options);
        
    }
    
    protected IHypervisor prepare(IHypervisor hypervisor, Properties options) throws Exception{
        
        String idHypervisor = MDA.BaseOptions.getProperty(MDA.HypervisorTag);
        String dsHypervisor = MDA.Configuration.getString(idHypervisor + ".description");
        
        hypervisor.setID(idHypervisor);
        hypervisor.setDescription(dsHypervisor);
        
        MDA.debug("Hypervisor " + hypervisor.getID() + " [" + hypervisor.getDescription() + "] has been created");
        
        List vmsID = MDA.Configuration.getList(idHypervisor + ".vm.id");      
        
        for(Object vmID : vmsID){
            
            MDA.BaseOptions.setProperty(MDA.VMTag, (String)vmID);
            
            IVirtualMachine machine = MDA.createVM(options);            
            
            hypervisor.addVirtualMachine((String)vmID, machine);
            
        }

        MDA.debug(hypervisor.getMachines().size() + " machine(s) were loaded in " + hypervisor.getID() + "."); 
        MDA.debug("The hypervisor " + hypervisor.getID() + "[" + hypervisor.getDescription() + "] has been successfully loaded.");                    
        
        return hypervisor;
    }
}